package com.zhonghui.dolo.manager.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhonghui.dolo.manager.dao.GoodsCategoryDao;
import com.zhonghui.dolo.manager.dao.GoodsDao;
import com.zhonghui.dolo.manager.dao.GoodsPriceDao;
import com.zhonghui.dolo.manager.model.Goods;
import com.zhonghui.dolo.manager.model.GoodsCategory;
import com.zhonghui.dolo.manager.model.GoodsPrice;
import com.zhonghui.dolo.manager.model.vo.*;
import com.zhonghui.dolo.manager.service.GoodsCategoryService;
import com.zhonghui.dolo.manager.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsCategoryDao goodsCategoryDao;
    @Autowired
    private GoodsPriceDao goodsPriceDao;
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Override
    public PageResultVo<List<GoodsVo>> queryGoodsPageResult(int pageNum, int pageSize, GoodsReqVo goodsReqVo) {
        Page<List<Goods>> page = PageHelper.startPage(pageNum, pageSize, true);
        List<Goods> goodsList = goodsDao.queryGoodsListByParam(goodsReqVo);

        List<GoodsVo> goodsVoList = new ArrayList<>();
        for (Goods goods : goodsList) {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(goods, goodsVo);
            GoodsCategory goodsCategory = goodsCategoryDao.getCategoryById(goods.getCategoryId());
            if (goodsCategory != null) {
                goodsVo.setCategory(goodsCategory.getCategoryName());
            }
            goodsVoList.add(goodsVo);
        }

        // 表分类
        PageResultVo<List<GoodsVo>> pageResultVo = new PageResultVo<>();
        pageResultVo.setRes(1);
        pageResultVo.setPageSize(pageSize);
        pageResultVo.setObj(goodsVoList);
        pageResultVo.setTotal(page.getTotal());
        return pageResultVo;
    }

    @Override
    public GoodsDetailVo getGoodsDetail(Integer goodsId) {
        Goods goods = goodsDao.getGoodsById(goodsId, true);
        if (goods == null) {
            return null;
        }
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        BeanUtils.copyProperties(goods, goodsDetailVo);
        // 查分类
        GoodsCategoryVo goodsCategoryVo = goodsCategoryService.getCategoryVoById(goods.getCategoryId());
        // 查商品规格价格
        List<GoodsPrice> goodsPriceList = goodsPriceDao.queryGoodsPriceListByGoodsId(goodsId);
        List<GoodsPriceVo> goodsPriceVoList = new ArrayList<>();
        for (GoodsPrice goodsPrice : goodsPriceList) {
            GoodsPriceVo goodsPriceVo = new GoodsPriceVo();
            BeanUtils.copyProperties(goodsPrice, goodsPriceVo);
            goodsPriceVoList.add(goodsPriceVo);
        }
        goodsDetailVo.setGoodsCategory(goodsCategoryVo);
        goodsDetailVo.setGoodsPriceList(goodsPriceVoList);
        return goodsDetailVo;
    }

    @Override
    public int updateGoods(GoodsEditVo goodsEditVo) {
        // t_goods
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsEditVo, goods);

        try {
            GoodsCategoryVo goodsCategoryVo = new ObjectMapper().readValue(goodsEditVo.getGoodsCategoryStr(), GoodsCategoryVo.class);
            if (goodsCategoryVo.getSecondId() != null) {
                goods.setCategoryId(goodsCategoryVo.getSecondId());
            } else {
                goods.setCategoryId(goodsCategoryVo.getFirstId());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        int result = goodsDao.updateByGoodsId(goods);
        // t_goods_price
        List<GoodsPrice> goodsPriceList = null;
        try {
            // json 字符串转 java bean
            goodsPriceList = new ObjectMapper().readValue(goodsEditVo.getGoodsPriceListStr()
                    , new ObjectMapper().getTypeFactory().constructParametricType(List.class, GoodsPrice.class));
        } catch (IOException e) {
            e.printStackTrace();
        }
        goodsPriceDao.updateGoodsPriceList(goodsPriceList);
        return result;
    }

    @Override
    public int updateGoodsV2(GoodsEditVo goodsEditVo) {
        // t_goods
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsEditVo, goods);
        if (goodsEditVo.getCategoryVo() != null) {
            if (goodsEditVo.getCategoryVo().getSecondId() != null) {
                goods.setCategoryId(goodsEditVo.getCategoryVo().getSecondId());
            } else {
                goods.setCategoryId(goodsEditVo.getCategoryVo().getFirstId());
            }
        }
        int result = goodsDao.updateByGoodsId(goods);

        if (goodsEditVo.getGoodsPriceVoList() != null) {
            goodsPriceDao.updateGoodsPriceList(goodsEditVo.getGoodsPriceVoList());
        }
        return result;
    }

    @Override
    public int addGoods(GoodsEditVo goodsEditVo) {
        // t_goods
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsEditVo, goods);
        Date now = new Date();
        goods.setCreateTime(now);
        goods.setUpdateTime(now);
        if (goodsEditVo.getCategoryVo() != null) {
            if (goodsEditVo.getCategoryVo().getSecondId() != null) {
                goods.setCategoryId(goodsEditVo.getCategoryVo().getSecondId());
            } else {
                goods.setCategoryId(goodsEditVo.getCategoryVo().getFirstId());
            }
        }
        int result = goodsDao.addGoods(goods);

        // 基础代码已完成
        List<GoodsPrice> goodsPriceVoList = goodsEditVo.getGoodsPriceVoList();
        if (goodsPriceVoList != null && goodsPriceVoList.size() > 0) {
            for (GoodsPrice goodsPrice : goodsPriceVoList) {
                goodsPrice.setGoodsId(goods.getGoodsId());
                goodsPrice.setCreateTime(now);
                goodsPrice.setUpdateTime(now);
            }
            goodsPriceDao.batchInsertGoodsPrice(goodsPriceVoList);
        }
        return result;
    }
}
